﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FindDepth.UI
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            run();
        }
        private Forest f = new Forest(10);

        private void run()
        {
            f.Link(2, 1);
            f.Link(3, 2);
            f.Link(5, 4);
            f.Link(4, 3);
            f.Link(7, 6);
            f.Link(9, 8);
            f.Link(8, 7);
            f.FindDepth(6);
            f.Link(6, 5);

            f.FindDepth(4);
            f.FindDepth(7);
            this.Invalidate();
        }

        private Boolean And(Boolean[] a)
        {
            Boolean r = true;
            for (int i = 1; i <= 9; i++)
                r = a[i] && r;
            return r;
        }

        private void FormMain_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Boolean[] a = new Boolean[10];
            List<int> parents = new List<int>();
            int j = 0;
            int[] pos = new int[10];

            while (!And(a))
            {
                if (parents.Count == 0)
                {
                    var r = from n in f.nodes
                            where n.IsRoot
                            select n;
                    int i = 0;
                    foreach (var item in r)
                    {
                        if (item.Name == 0)
                            continue;
                        a[item.Name] = true;
                        g.DrawRectangle(Pens.Black, new Rectangle(10, 30 * i + 10, 20, 20));
                        g.DrawString(item.Name.ToString(), Font, Brushes.Black, 10, 30 * i + 10);
                        parents.Add(item.Name);
                        i++;
                        pos[item.Name] = i;
                    }
                }
                else
                {
                    var r = from n in f.nodes
                            where parents.Contains(n.Parent) && !n.IsRoot
                            orderby n.Parent
                            select n;
                    int i = 0;
                    List<int> temp = new List<int>();
                    foreach (var item in r)
                    {
                        a[item.Name] = true;
                        Int32 left = 30 * j + 10;
                        Int32 top = 30 * i + 10;
                        g.DrawRectangle(Pens.Black, new Rectangle(left, top, 20, 20));
                        g.DrawString(item.Name.ToString(), Font, Brushes.Black, left, top);
                        g.DrawLine(Pens.Red, left + 10, top + 10, left - 20,
                            pos[item.Parent] * 30 - 10);
                        temp.Add(item.Name);
                        i++;
                        pos[item.Name] = i;

                    }
                    parents = temp;
                }
                j++;
            }





        }
    }
}
